package com.ubxtech.core.exception;


/**
 * @author Victor.Xiao
 * @since 2020-01-15 17:04
 **/
public enum ErrorCodeCore implements ErrorCode {
    /*========================================================================================================
     * 共有错误码：适用于所有 API，一般描述与业务无关的错误或者是多数业务都可能出现的错误
     *========================================================================================================
     */

    /** 文件相关 750开始 */
    FILE_READ_AUDIO_ERROR(500, 759, "解析音频文件失败"),
    FILE_EXISTS_ERROR(500, 758, "文件已经存在"),
    FILE_UPLOAD_ERROR(500, 757, "文件上传失败"),
    FILE_CONTENT_EMPTY(500, 756, "文件内容为空"),
    FILE_CHUNK_NOT_EXISTS(500, 755, "文件分片不存在"),
    FILE_MERGE_ERROR(500, 754, "文件合并有误"),
    FILE_NO_PERMISSION(500, 753, "没有文件权限"),
    FILE_NOT_EXISTS(500, 752, "文件不存在"),
    FILE_TYPE_ERROR(500, 751, "文件格式有误"),
    FILE_READ_ERROR(500, 750, "读取文件有误"),

    /** 签名相关 700开始 */
    SIGN_CONTENT_ERROR(500, 705, "签名格式不正确"),
    SIGN_APP_ID_NOT_EXISTS_ERROR(500, 704, "授权的appId不存在"),
    SIGN_TIME_ERROR(500, 703, "签名时间校验失败"),
    SIGN_MD5_ERROR(500, 702, "签名MD5校验失败"),
    SIGN_ERROR(500, 701, "签名校验失败"),


    /** 数据库相关 600开始 */
    DB_QUERY_ERROR(500, 606, "sql执行有误"),
    DUPLICATE_KEY_NAME(500, 605, "名称已存在"),
    DATA_NOT_EXISTS(500, 604, "数据不存在"),
    UPDATE_SQL_CONSTRAINT_ERROR(500, 603, "修改数据依赖有误"),
    UPDATE_SQL_ERROR(500, 602, "修改数据出错"),
    NO_PERMISSION(500, 601, "没有权限，请联系管理员授权"),
    DUPLICATE_KEY(500, 600, "数据已存在"),


    /*========================通用错误================================================================================ */
    /** 非法请求：参数格式非法、参数字段缺失&类型错误&非法值、非法 HTTP 头等 */
    BAD_REQUEST(400, 400, "Invalid parameter"),
    /** 鉴权不通过：Token 格式非法、过期等 */
    UNAUTHORIZED(401, 401, "Unauthorized"),
    /** 参数对象中的非法字段，缺少字段、字段类型错误、字段取值非法 */
    INVALID_PARAM_FILED(400, 400, "Invalid fields in the parameter object"),
    /** 禁止访问，没有权限 */
    FORBIDDEN(403, 403, "Forbidden"),
    /** 资源未找到 */
    NOT_FOUND(404, 404, "Not found"),
    /** 连接超时 */
    CONNECT_TIME_OUT(408, 408, "connection timed out"),
    /** 限流 */
    SENTINEL_EXCEPTION(429, 429, "too many request"),
    /** 服务器内部错误 */
    INTERNAL_SERVER_ERR(500, 500, "Internal server error"),
    /** 网关错误 */
    GATEWAY_ERROR(502, 502, "Bad Gateway"),
    /** 网关请求超时 */
    GATEWAY_REQUEST_TIME_OUT(504, 504, "Gateway timeout")
    ;

    /*======================================================================================================== */

    /**
     * HTTP状态码 jakarta.servlet.http.HttpServletResponse.SC_BAD_REQUEST 例举了错误码
     */
    private int httpStatus;

    /**
     * 错误码
     */
    private int code;

    /**
     * 错误消息
     */
    private String message;


    public int getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
    }

    @Override
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    private ErrorCodeCore(int httpStatus, int code, String message) {
        this.httpStatus = httpStatus;
        this.code = code;
        this.message = message;
    }

    public static ErrorCodeCore parseByMessage(String message) {

        for (ErrorCodeCore errorCodeEnum : ErrorCodeCore.values()) {
            if (errorCodeEnum.getMessage().equals(message)) {
                return errorCodeEnum;
            }
        }
        return null;
    }
}
